package com.leyou.controller;

import com.leyou.service.GoodsHtmlService;
import com.leyou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Controller
@RequestMapping("portal/item")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    /**
     * 跳转到商品详情页
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/{id}.html")
    public String toItemPage(Model model, @PathVariable("id") Long id) {
        // 加载所需的数据
        Map<String, Object> map = goodsService.loadData(id);
        System.out.println("请求之后最终的数据：" + map.toString());
        // 把数据放入数据模型
        model.addAllAttributes(map);

        // 页面静态化，主线程不会等待，次线程持续执行静态化
        this.goodsHtmlService.asyncExcute(id, map);
        return "item";
    }

}
